package ExtendDemo.demo10;

import java.io.*;
import java.net.Socket;

public class ServerThread implements Runnable {
    private Socket s;

    public ServerThread(Socket s) {
        this.s = s;
    }

    @Override
    public void run() {
        //接收数据写到文本文件
        try {
            BufferedReader br=new BufferedReader(new InputStreamReader(s.getInputStream()));
//            BufferedWriter bw=new BufferedWriter(new FileWriter("copy.txt"));
            //解决名称冲突问题
            int count =0;
            File file = new File("copy" + count + ".txt");
            while (file.exists()){
                count++;
                file = new File("copy" + count + ".txt");
            }
            BufferedWriter bw=new BufferedWriter(new FileWriter(file));
            String line;

            while ((line= br.readLine())!=null){
                bw.write(line);
                bw.newLine();
                bw.flush();
            }
            BufferedWriter bw2=new BufferedWriter(new OutputStreamWriter(s.getOutputStream()));
            bw2.write("文件上传成功");
            bw2.newLine();
            bw2.flush();

            s.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
